﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using FineUI;
using SDLITS.Model;
using SDLITS.Common;
using MySoft.Data;
using System.Data;
using System.Data.SqlClient;

namespace SDLITS.Web.Driver
{
    public partial class DealCardLogManage : CommonBase
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                txt_end.SelectedDate = DateTime.Now;
                BindCom();
                BindDriver();
                ModelLoginUser user = Session["LoginUser"] as ModelLoginUser;
                if (user.RoomID != -1)
                {
                    drp_company.Hidden = true;
                    drp_driver.Hidden = true;
                    hidroomid.Text = user.RoomID.ToString();
                    BindGrid();
                }
            }
        }
        private void BindCom()
        {
            List<V_CompanyARU> coms = SDDbSession.Default.From<V_CompanyARU>().Where(V_CompanyARU._.StatusID != 3).ToList();
            foreach (var row in coms)
            {
                ListItem li = new ListItem();
                li.Text = String.Format("{0} ({1})", row.CompanyName, row.AreaName + "-" + row.RoomName);
                li.Value = row.CompanyID.ToString();
                drp_company.Items.Add(li);
            }
            drp_company.Items.Insert(0, new ListItem("--全部公司--", ""));
        }
        private void BindDriver()
        {
            SqlParameter[] sp = new SqlParameter[]{
                    new SqlParameter("@SPAction","SelectAllDriver")
                };
            DataSet ds = SDDbSession.Default.FromProc("TableMemberInfo").AddParameter(sp).ToDataSet();
            if (ds != null & ds.Tables.Count > 0)
            {
                drp_driver.Items.Clear();
                ListItem li = new ListItem("--全部驾驶员--", "");
                drp_driver.Items.Add(li);
                drp_driver.Items[0].Selected = true;
                for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
                {
                    ListItem lis = new ListItem(ds.Tables[0].Rows[i]["Value"].ToString(), ds.Tables[0].Rows[i]["ID"].ToString());
                    drp_driver.Items.Add(lis);
                    if (i == 100)//数据太多，卡死
                    {
                        break;
                    }
                }

            }
        }
        private void BindGrid()
        {
            var startDate = new DateTime?();
            if (!string.IsNullOrEmpty(txt_start.Text))
            {
                startDate = Convert.ToDateTime(txt_start.Text);
            }
            int s = int.Parse(hidroomid.Text);
            if (drp_company.SelectedValue != "")
            {
                Company som = SDDbSession.Default.From<Company>().Where(Company._.CompanyID == drp_company.SelectedValue).ToFirst();
                if (som != null)
                {
                    if (som.RoomID.HasValue)
                        s = som.RoomID.Value;
                }
            }

            SqlParameter[] sp = new SqlParameter[]{
                    new SqlParameter("@RoomID",s),
                    new SqlParameter("@StartDate",startDate),
                    new SqlParameter("@EndDate",Convert.ToDateTime(txt_end.Text)),
                    new SqlParameter("@CutoverDriverIDNumber",drp_driver.SelectedValue),
                    new SqlParameter("@IsCutover",true),
                    new SqlParameter("@SPAction","SelectByDateRange")
                };
            DataSet ds = SDDbSession.Default.FromProc("TableLEDMessage").AddParameter(sp).ToDataSet();
            if (ds != null && ds.Tables.Count > 0)
            {
                Grid1.DataSource = ds.Tables[0];
                Grid1.DataBind();

                tool_txt.Text = "总交割单 " + ds.Tables[0].Rows.Count + " 笔";
            }
        }
        private bool SetPrintSession(int id)
        {
            SqlParameter[] sp = new SqlParameter[]{
                    new SqlParameter("@MessageID",id),
                    new SqlParameter("@SPAction","SelectByMessageIDForDealed")
                };
            DataSet Ds = SDDbSession.Default.FromProc("TableLEDMessage").AddParameter(sp).ToDataSet();
            if (Ds.Tables.Count > 0 && Ds != null && Ds.Tables[0].Rows.Count > 0)
            {
                DataRow dr = Ds.Tables[0].Rows[0];
                var mp = new ModelPrint
                {
                    MemCardID = dr["MemCardID"] is DBNull ? "" : dr["MemCardID"].ToString(),
                    MemCardNo = dr["MemCardNo"] is DBNull ? "" : dr["MemCardNo"].ToString(),
                    StartDate = dr["StartDate"] is DBNull ? "" : dr["StartDate"].ToString(),
                    ExpireDate = dr["ExpireDate"] is DBNull ? "" : dr["ExpireDate"].ToString(),
                    LeftCash = dr["LeftCash"] is DBNull ? "" : dr["LeftCash"].ToString(),

                    RegistID = dr["RegistID"] is DBNull ? "" : dr["RegistID"].ToString(),
                    IDNumber = dr["IDNumber"] is DBNull ? "" : dr["IDNumber"].ToString(),
                    Name = dr["Name"] is DBNull ? "" : dr["Name"].ToString(),
                    Address = dr["IDAddress"] is DBNull ? "" : dr["IDAddress"].ToString(),
                    Sex = dr["Sex"] is DBNull ? "" : dr["Sex"].ToString(),
                    Birthday = dr["Birthday"] is DBNull ? "" : dr["Birthday"].ToString(),
                    Cell = dr["Cell"] is DBNull ? "" : dr["Cell"].ToString(),
                    Phone = dr["Phone"] is DBNull ? "" : dr["Phone"].ToString(),
                    DLName = dr["DLName"] is DBNull ? "" : dr["DLName"].ToString(),
                    DLSex = dr["DLSex"] is DBNull ? "" : dr["DLSex"].ToString(),
                    DLNumber = dr["DLNumber"] is DBNull ? "" : dr["DLNumber"].ToString(),
                    DLAddress = dr["DLAddress"] is DBNull ? "" : dr["DLAddress"].ToString(),
                    CarNo = dr["CarNo"] is DBNull ? "" : dr["CarNo"].ToString(),
                    CarEnginNo = dr["CarEnginNo"] is DBNull ? "" : dr["CarEnginNo"].ToString(),
                    CarIDNo = dr["CarIDNo"] is DBNull ? "" : dr["CarIDNo"].ToString(),
                    CarOwnerName = dr["CarOwnerName"] is DBNull ? "" : dr["CarOwnerName"].ToString(),
                    CarOwnerAddr = dr["CarOwnerAddr"] is DBNull ? "" : dr["CarOwnerAddr"].ToString(),
                    GCarNo = dr["GCarNo"] is DBNull ? "" : dr["GCarNo"].ToString(),
                    GCarIDNo = dr["GCarIDNo"] is DBNull ? "" : dr["GCarIDNo"].ToString(),
                    GoodName = dr["GoodName"] is DBNull ? "" : dr["GoodName"].ToString(),
                    AmountUnit =
                        string.Format("{0}({1})", dr["Amount"] is DBNull ? "" : dr["Amount"].ToString(),
                                      dr["UnitType"] is DBNull ? "" : dr["UnitType"].ToString()),
                    CarInfo = dr["CarInfo"] is DBNull ? "" : dr["CarInfo"].ToString(),
                    TransPrice = dr["TransPrice"] is DBNull ? "" : dr["TransPrice"].ToString(),
                    StartPlace = dr["StartPlace"] is DBNull ? "" : dr["StartPlace"].ToString(),
                    EndPlace = dr["EndPlace"] is DBNull ? "" : dr["EndPlace"].ToString(),
                    CompanyIDNumber =
                        dr["CutoverCompanyIDNumber"] is DBNull
                            ? ""
                            : dr["CutoverCompanyIDNumber"].ToString(),
                    Room = dr["Room"] is DBNull ? "" : dr["Room"].ToString(),
                    CompanyName = dr["CompanyName"] is DBNull ? "" : dr["CompanyName"].ToString(),
                    ContactPerson = dr["ContactPerson"] is DBNull ? "" : dr["ContactPerson"].ToString(),
                    CompanyPhone = dr["CompanyPhone"] is DBNull ? "" : dr["CompanyPhone"].ToString(),
                    PrintTime = DateTime.Now.ToString()
                };
                Session["ModelPrint"] = mp;
                return true;
            }
            return false;
        }

        protected void Grid1_PageIndexChange(object sender, GridPageEventArgs e)
        {
            Grid1.PageIndex = e.NewPageIndex;
            BindGrid();
        }

        protected void drp_company_SelectedIndexChanged(object sender, EventArgs e)
        {
            string roomID = "0";
            if (!string.IsNullOrEmpty(drp_company.SelectedValue))
            {
                try
                {
                    roomID = SDDbSession.Default.From<Company>().Where(Company._.CompanyID == drp_company.SelectedValue).ToFirst().RoomID.ToString();
                }
                catch (Exception) { roomID = "0"; }
            }
            if (string.IsNullOrEmpty(roomID))
            {
                roomID = "0";
            }
            hidroomid.Text = roomID;
            BindGrid();
        }

        protected void Grid1_RowDataBound(object sender, GridRowEventArgs e)
        {
            //e.Values[9] = "<a href='javascript:;' onclick=\"window.open('DealCardPrint.aspx', '打印交割单', 'height=600,top=10,width=880,toolbar=no,menubar=no,scrollbars=yes, resizable=no,location=no, status=no');\">查看</a>";
        }

        protected void Grid1_RowCommand(object sender, GridCommandEventArgs e)
        {
            int id = int.Parse(Grid1.DataKeys[e.RowIndex][0].ToString());
            if (e.CommandName == "view")
            {
                if (SetPrintSession(id))
                {
                    PageContext.RegisterStartupScript("window.open('DealCardPrint.aspx', '打印交割单', 'height=600,top=10,width=880,toolbar=no,menubar=no,scrollbars=yes, resizable=no,location=no, status=no')");
                }
                else
                {
                    Alert.ShowInTop("需要打印的数据不完整！");
                }
            }
        }

        protected void btn_search_Click(object sender, EventArgs e)
        {
            BindGrid();
        }
    }
}
